package com.situ.mall.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.manager.entity.Category;
import com.situ.mall.manager.mapper.CategoryMapper;
import com.situ.mall.manager.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//实现service接口
//添加service注解
@Service
public class CategoryServiceImpl implements CategoryService {

    //注入mapper层对象
    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Override
    public int add(Category category) throws Exception {
        //验证参数
        if (category.getName()==null || category.getName().length()==0) {
            throw new Exception("分类名不能为空");
        }
        if (category.getRecom()==null) {
            throw new Exception("推荐不能为空");
        }
        //逻辑处理

        //访问数据库
        return categoryMapper.insert(category);
    }

    @Override
    public int remove(int id) throws Exception {
        Category category = categoryMapper.selectById(id);
        if (category == null) {
            throw new Exception("id不存在");
        }
        return categoryMapper.delete(id);
    }

    @Override
    public int edit(Category category) throws Exception {
        Category sCategory = categoryMapper.selectById(category.getId());
        if (sCategory==null) {
            throw new Exception("id不存在");
        }
        return categoryMapper.update(category);
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public List<Category> search(Category category) {
        return categoryMapper.select(category);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Category category) {
        //分三步
        //1.启动分页查询
        PageHelper.startPage(page,limit);

        //2.调用Mapper
        List<Category> list =  categoryMapper.select(category);

        //3.构造PageInfo
        PageInfo pageInfo = new PageInfo(list);

        return pageInfo;

    }


}
